<?php

namespace App\Http\Controllers;

use App\Models\MyMessage;

use App\Models\User;
use App\Models\MakerUser;
use App\Http\Libs\Helper_Huanxin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use \DB;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Http\RedirectResponse;
use App\Models\Maker;
use App\Models\Zone;
use App\User as AppUser;
use App\Models\Notice;
use App\Http\Requests\MyMessageRequest;

class MyMessageController extends CommonController
{
    /**
     * 获取app消息管理列表
     *
     */
    public function getIndex()
    {
        $lists = MyMessage::with('user')->orderBy('id', 'asc')->paginate(10);

        return view('mymessage.index')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();

        $sql = MyMessage::with('user')->orderBy('id', 'asc');

        if (isset($data['title']) && $data['title'] != '') {
            $sql->where('title', 'like', '%' . $data['title'] . '%');
        }

        if (isset($data['content']) && $data['content'] != '') {
            $sql->orWhere('content', 'like', '%' . $data['content'] . '%');
        }

        if (isset($data['type']) && $data['type'] != '') {
            $sql->where('type', $data['type']);
        }

        $lists = $sql->paginate(10);

        return view('mymessage.search')->with("lists", $lists)->with("title", $data['title'])
            ->with("content", $data['content'])->with("type", $data['type'])->with("actions", $this->actions);
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        $users = User::all();

        return view('mymessage.create')->with('users', $users);
    }

    /**
     *执行一个创建
     */
    public function postCreate(MyMessageRequest $request)
    {
        $data = $request->input();
        $now  = date('Y-m-d H:i:s', time());
        if(isset($data['url'])&&$data['url']!=''){
            $url='<a href="'.$data['url'].'">
	   <div class="lightgray2 mt10">
	   	    <span class="">查看信息</span>
	   	    <span class="sj_right r middle"></span>
	   </div>
	   </a>';
        }else{
            $url='';
        }
        $data['content'] = '<section class="white-bg inforBlock content">
	   <h1 class="f133 mt10">'.$data['title'].'</h1>
	   <div class="mt10 mb10">
	   	  <p class="lightgray2 mt5">'.$now.'</p>
	   	  <p class="dark2_gray mt5">'.$data['content'].'</p>
	   </div>
	   <div class="borderLine"></div>
	   '.$url.'
    </section>';

    MyMessage::create(['title'=>$data['title'], 'uid'=>$data['uid'], 'content'=>$data['content'],'message_type'=>$data['message_type'], 'url'=>$data['url']]);

        return AjaxCallbackMessage('创建成功',true, url('mymessage/index'));
    }

    /**
     * 获取编辑页面
     */
    public function getEdit()
    {
        return view('mymessage.edit');
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(Request $request)
    {
        $data = $request->input();
    }
}
